package com.qkl.common.publics.web.controller;

import com.qkl.common.publics.dto.BusinessDTO;
import com.qkl.common.publics.utils.ErrorCodeUtils;
import com.qkl.common.publics.utils.Result;

public class ControllerHelper {

    public static Result toQueryResult(BusinessDTO businessDTO){
        return toResult(businessDTO, ErrorCodeUtils.QUERY_FAIL);
    }

    public static Result toOperateResult(BusinessDTO businessDTO){
        return toResult(businessDTO,ErrorCodeUtils.OPERATE_FAIL);
    }

    private static Result toResult(BusinessDTO businessDTO,Integer errorCode){
        if( !businessDTO.isSucess()){
            Integer code = businessDTO.getCode();
            if(null != code){
                errorCode = code;
            }
            return Result.error(errorCode,businessDTO.getMessage());
        }
        return Result.success(businessDTO.getBusinessObject(),businessDTO.getMessage());
    }

}
